$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'business/instructions/list',
        datatype: "json",
        colModel: [			
			/*{ label: 'id', name: 'id', index: 'ID', width: 50, key: true },*/
			{ label: '状态', name: 'receiveStatus', index: 'RECEIVE_STATUS', width: 50 ,formatter: function(value, options, row){
				switch(value){
					case '0' : return '<label class=" ">未签收</label>';
					case '1' : return '<label class=" ">已签收</label>';
					case '2' : return '<label class=" ">已完成</label>';
					case '3' : return '<label class=" ">进行中</label>';
					default : return '<label class=" ">未知</label>';
				}
				}},
			{ label: '标题', name: 'titleName', index: 'TITLE_NAME', width: 80 }, 			
			{ label: '发布时间', name: 'createTime', index: 'CREATE_TIME', width: 80 }, 			
			{ label: '发布单位', name: 'name', index: 'NAME', width: 80 }, 
			{ label: '发布范围', name: 'fbfw', index: 'FBFW', width: 80 }, 	
//			{ label: '截止日期', name: 'hysj', index: 'HYSJ', width: 80 }, 			
			/*{ label: '督察描述', name: 'zlxq', index: 'ZLXQ', width: 80 }, 			
			{ label: '项目状态(0：立项中，1：进行中，2：办结中，3：已办结)', name: 'zlzt', index: 'ZLZT', width: 80 },*/ 			
			{ label: '重要度', name: 'level', index: 'LEVEL', width: 80 , formatter: function(value, options, row){
				return value === 1 ? 
						'<span class="label label-danger">紧急</span>' : 
						'<span class="label label-success">普通</span>';
				}}			
			/*{ label: '是否短信通知(0：通知，1：不通知)', name: 'msgFlag', index: 'MSG_FLAG', width: 80 }, 			
			{ label: '删除标志(0：未删除，1：已删除)', name: 'deleteFlag', index: 'DELETE_FLAG', width: 80 }, 			
			{ label: '创建人ID', name: 'createUser', index: 'CREATE_USER', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80 }, 			
			{ label: '更新人ID', name: 'updateUser', index: 'UPDATE_USER', width: 80 }, 			
			{ label: '更新时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80 }*/			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            titleName: null
        },
		showList: true,
		showdetail: true,
		title: null,
		instructions: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.instructions = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.instructions.id == null ? "business/instructions/save" : "business/instructions/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.instructions),
			    success: function(r){
			    	if(r.code == 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "business/instructions/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.instructions = {};
			$.get(baseURL + "business/instructions/info/"+id, function(r){
				if(r.code == "0"){
					vm.instructions = r.instructions;
					vm.showList = false;
					vm.showdetail = false;
				}else{
					alert(r.msg);
				}
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'titleName': vm.q.titleName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});